package com.atguigu.gulimail.product.service.impl;

import com.atguigu.gulimail.product.entity.BrandEntity;
import com.atguigu.gulimail.product.entity.CategoryEntity;
import com.atguigu.gulimail.product.service.BrandService;
import com.atguigu.gulimail.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimail.product.dao.CategoryBrandRelationDao;
import com.atguigu.gulimail.product.entity.CategoryBrandRelationEntity;
import com.atguigu.gulimail.product.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao,
        CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    CategoryService categoryService;
    @Autowired
    BrandService brandService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils catelogList(Map<String, Object> params) {

        final IPage<CategoryBrandRelationEntity> page1 =
                new Query<CategoryBrandRelationEntity>().getPage(params);
        final QueryWrapper<CategoryBrandRelationEntity> wrapper = new QueryWrapper<>();
        if (params.containsKey("brandId")) {
            final String key = (String) params.get("brandId");
            wrapper.eq("brand_id", key);
        }
        if (params.containsKey("catelogId")) {
            final Long key = (Long) params.get("catelogId");
            wrapper.eq("catelog_id", key);
        }

        IPage<CategoryBrandRelationEntity> page = this.page(
                page1,
                wrapper
        );
        return new PageUtils(page);
    }

    @Override
    public void saveRelation(CategoryBrandRelationEntity categoryBrandRelation) {

        final CategoryEntity categoryEntity =
                categoryService.getById(categoryBrandRelation.getCatelogId());
        final BrandEntity brandEntity = brandService.getById(categoryBrandRelation.getBrandId());
        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());
        this.save(categoryBrandRelation);


    }
}